#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <opencv.hpp>
#include "frasserver.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    // 注册信号相关的非QT自带数据类型以便在信号中使用
    qRegisterMetaType<cv::Mat>("cv::Mat&");
    qRegisterMetaType<int64_t>("int64_t");
    // 数据库相关操作
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  // 连接数据库
    db.setDatabaseName("server.db");                         // 设置数据库名称
    // 打开数据库
    if (!db.open()) {
        qDebug() << db.lastError().text() << '\n';
        return -1;
    }
    // 创建员工信息表格（只创建一次）
    QString sql = "CREATE TABLE IF NOT EXISTS staff_info ("
                  "    staff_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
                  "    staff_name VARCHAR(50) NOT NULL,"
                  "    staff_face_id INTEGER UNIQUE,"
                  "    staff_img_file_path TEXT,"
                  "    staff_birth DATE,"
                  "    staff_sex CHAR(1),"
                  "    staff_phone VARCHAR(15),"
                  "    staff_addr TEXT,"
                  "    staff_depa TEXT"
                  ");";
    QSqlQuery query;
    if (!query.exec(sql)) {
        qDebug() << db.lastError().text() << '\n';
    }
    // 创建考勤表格（注意考勤可能有多次，因此staff_id不应是UNIQUE的
    sql = "CREATE TABLE IF NOT EXISTS attendance_info ("
          "    att_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
          "    staff_id INTEGER NOT NULL,"
          "    att_time TIMESTAMP NOT NULL DEFAULT(datetime('now','localtime')),"
          "    remark TEXT"
          ");";
    if (!query.exec(sql)) {
        qDebug() << db.lastError().text() << '\n';
    }
    // 启动服务端窗口
    FrasServer w;
    w.show();
    return a.exec();
}
